<?php
/**
 * 角色的管理
 */
namespace app\admin\controller;
use app\admin\model\RoleModel;
use think\Request;
class Role extends Base
{
    protected function _initialize()
    {
        $this->model = new RoleModel();
    }
	/**
	 * 角色 列表
	 * @return [type] [description]
	 */
	public function index(){
        $config = array();
        $config['path'] = url('Card/index',input('param.'));

		$where = [];

		if(input('name')){
			$where['name'] = array('like','%'.input('name').'%');
		}

		$list = model("roleModel")->where($where)->paginate(25,false,$config);
		$this->assign('list',$list);

		return $this->fetch();
	}

	/**
	 * 角色添加和修改
	 * @return [type] [description]
	 */
	public function oppt(){
		$m = model('roleModel');
		if($this->request->isPost()){
            $roleId = intval($_REQUEST['role']['id']);
            $_REQUEST['role']['rule'] = implode(",", $_REQUEST['role']['rule']);
            if($roleId){
            	// var_dump($_REQUEST['role']);
            	$_REQUEST['role']['update_time'] = time();
                $res = $m->where(array('id'=>$roleId))->update($_REQUEST['role']);
            }else{
                unset($_REQUEST['role']['id']);
                $res = $m->data($_REQUEST['role'])->save();
            }
            if($res!==false){
                $this->success('操作成功');
            }else{
                $this->success('操作失败');
            }
		}else{
			$id = input('id',0,'intval');
			if($id){
				$info = $m->where(array('id'=>$id))->find();
				$info['rule'] = explode(",", $info['rule']);
				// print_r($info);
				$this->assign("info",$info);
			}
			//找 所有节点
			$rule = model('authModel')->select();
			if($rule){
				$temp = array();
				foreach ($rule as $v) {
					$temp[$v['pid']][$v['id']] = $v;
				}
				$rule = $temp;
			}
			$this->assign('rule',$rule);
			return view();
		}
	}
}